﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table width="98%" class="shadow">
        <tr>
            <td colspan="2" style="background-color: #E46C0A; color: White; font-size: xx-large;
                text-align: center;">
                New Sample &nbsp; <span class="fontTh">(ผ้าใบทดลอง)</span>
            </td>
        </tr>
        <tr class="rowa">
            <td class="head cell text-right" style="width: 35%; padding: 10px 0px;">
                Size &nbsp;<span class="fontTh">ขนาดผ้าใบ</span>
            </td>
            <td class="cell">
                <input type="text" id="txtSize" maxlength="50" />
            </td>
        </tr>
        <tr class="rowa">
            <td class="head cell text-right" style="padding: 10px 0px;">
                Forming&nbsp;<span class="fontTh">เครื่องที่สร้าง</span>
            </td>
            <td class="cell">
                <input type="text" id="txtForming" maxlength="5" />
            </td>
        </tr>
        <tr class="rowa">
            <td class="head cell text-right" style="padding: 10px 0px;">
                Topped Cord A <span class="fontTh">ผ้าใบ A</span>
            </td>
            <td class="cell">
                <input type="password" id="txtInputA" cols="A" chk="" value="" onkeydown="checkEnter($(this));" />
            </td>
        </tr>
        <tr class="rowa">
            <td class="head cell text-right" style="padding: 10px 0px;">
                Topped Cord B <span class="fontTh">ผ้าใบ B</span>
            </td>
            <td class="cell">
                <input type="password" id="txtInputB" cols="B" chk="" value="" onkeydown="checkEnter($(this));" />
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
    <section class="featured">
 <div class="content-wrapper" style="height:40px">
            <hgroup class="title float-left">
            <h1>Bias Head Computer</h1>
                <h2  style="padding-left:55px;">BIAS NO.<%: TireBias.Models.SessionBag.Current.biasno %></h2>      
               
            </hgroup> 
             <hgroup class="title float-right">
                <h3>
         Name :
         <% if (Request.IsAuthenticated)
            { %>
         <%:  User.Identity.Name %>
         <% } %></h3>
            </hgroup>   
 </div>
 </section>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="TableContent" runat="server">
    <div class="float-left" style="margin-left: 50px;">
        <input type="button" id="back" value="BACK" class="btnFooter" style="height: 75px;
            width: 350px;" />
    </div>
    <%--//************ dialog *******************//--%>
    <div id="dialog-b" title="Use Tire Cord">
        <table style="width: 98%" class="fontTB">
            <tr class="rowa">
                <td class="head cell text-right" style="padding: 10px 0px; width: 30%">
                    Emp code
                </td>
                <td class="cell">
                    <input type="text" id="txtempnm" style="width: 75%" maxlength="6" />
                </td>
            </tr>
            <tr class="rowa">
                <td class="head cell text-right" style="padding: 10px 0px;">
                    Password
                </td>
                <td class="cell">
                    <input type="password" id="txtpass" style="width: 75%" />
                </td>
            </tr>
            <tr class="rowa">
                <td class="head cell text-right" style="padding: 10px 0px;">
                    Short Text
                </td>
                <td class="cell">
                    <input type="text" id="txtshort" style="width: 75%" maxlength="30" />
                </td>
            </tr>
        </table>
    </div>
    <div id="dialog-c" title="">
        <table width="95%">
            <tr class="head shadow" style="height: 35px">
                <td class="cell" style="width: 40%">
                    ITEM
                </td>
                <td class="cell">
                    A
                </td>
                <td class="cell">
                    B
                </td>
            </tr>
            <tr class="rowa">
                <td class="head cell" style="text-align: left;">
                    Topped Cord<br />
                    <span class="fontTh">ผ้าใบ</span>
                </td>
                <td class="cell" id="dCodeA">
                </td>
                <td class="cell" id="dCodeB">
                </td>
            </tr>
            <tr class="rowa cell">
                <td style="text-align: left;" class="head cell">
                    Tire Cord Lot
                    <br />
                    <span class="fontTh">ล็อคที่ผลิต</span>
                </td>
                <td id="dLotA" class="cell">
                </td>
                <td id="dLotB" class="cell">
                </td>
            </tr>
            <tr class="rowa cell">
                <td style="text-align: left;" class="head cell">
                    Expired Date
                    <br />
                    <span class="fontTh">วันที่หมดอายุ</span>
                </td>
                <td id="dExpA" class="cell">
                </td>
                <td id="dExpB" class="cell">
                </td>
            </tr>
            <tr class="rowa">
                <td style="text-align: left;" class="head cell">
                    Rollno
                    <br />
                    ม้วนที่
                </td>
                <td id="dRollA" class="cell">
                </td>
                <td id="dRollB" class="cell">
                </td>
            </tr>
        </table>
        <input type="hidden" value="" id="txtreaMat" />
    </div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ScriptsSection" runat="server">
    <style type="text/css">
        input[type='text'], input[type='password']
        {
            font-size: 1em;
            height: 2em;
            line-height: 2.5em;
            width: 90%;
        }
        .nomat
        {
            color: Red !important;
        }
    </style>
    <script type="text/javascript">


        function checkEnter(obj) {
            if (event.keyCode == 13) {
                var nm = (obj.attr("id"));
                if (nm == "txtInputA") $("#txtInputB").focus();
                if (nm == "txtInputB") $("#txtInputB").blur();
            }
        }


        $(function () {

            var newtitle;
            var dialog;
            $("#txtSize").focus();
            function GET_Barcode(obj) {
                var myArray = obj.val().split('+');

                if (!(7 >= myArray.length <= 8)) {
                    return false;
                }

                var pt = $.trim(myArray[6 + (myArray.length - 7)]);
                if ($.trim(obj.attr("cols")) != pt) {
                    return false;
                }
                obj.attr("chk", "T")
                return true;


            }

            var noMat = "";

            function Check_MAT() {
                myArrayA = $("#txtInputA").val().split("+");
                myArrayB = $("#txtInputB").val().split("+");
                var bln = true;

                if ($.trim(myArrayA[0]) != $.trim(myArrayB[0])) {
                    noMat = "Topped Cord";
                    bln = false;
                    $("#dCodeA").addClass("nomat"); $("#dCodeB").addClass("nomat");

                } else if ($.trim(myArrayA[1]) != $.trim(myArrayB[1])) {
                    noMat = "Lot " + myArrayB[1];
                    bln = false;
                    $("#dLotA").addClass("nomat");
                    $("#dLotB").addClass("nomat");
                }
                else if ($.trim(myArrayA[5]) != $.trim(myArrayB[5])) {
                    noMat = "Expired Date" + myArrayB[5];
                    $("#dExpA").addClass("nomat"); $("#dExpB").addClass("nomat");
                    bln = false;
                }
                else if ($.trim(myArrayA[2]) != $.trim(myArrayB[2])) {
                    noMat = "Rollno " + myArrayB[2];
                    $("#dRollA").addClass("nomat"); $("#dRollB").addClass("nomat");
                    bln = false;
                }

                return bln;

            }


            $("#txtInputA,#txtInputB,#txtSize,#txtForming").change(function () {
                $(this).val($(this).val().toUpperCase())
                CheckTxt($(this));
            });

            function CheckTxt($this) {

                var objId = $this.attr("id");
                var Exp = false;
                if (objId == "txtInputA" || objId == "txtInputB") {
                    noMat = "";
                    if (!GET_Barcode($this)) {
                        alert("ข้อมูลไม่ถูกต้อง");
                        $this.val("");
                        $this.removeAttr("chk");
                        $this.focus();
                        return false;
                    }

                    if (objId == "txtInputA") {
                        var myArray = $this.val().split('+');
                        if (myArray[7] == "A") {

                            $.post('<%=Url.Action("ConvertDate","BiasHead") %>' + '?sdate=' + myArray[4], function (data) {
                                Exp = !data.bln;

                                if (Exp) {
                                    $("#dExpA").addClass("nomat");
                                }
                            }); //post
                        } // if
                    }
                }

                if ($("#txtSize").val() == "") {
                    return;
                }
                if ($("#txtForming").val() == "") {
                    return;
                }

                if ($("#txtInputA").val() == "") {
                    return;
                }
                if ($("#txtInputB").val() == "") {
                    return;
                }



                if ($("#txtInputA").attr("chk") != "T") {
                    alert('A');
                    return;
                }
                if ($("#txtInputB").attr("chk") != "T") {
                    alert('B');
                    return;
                }


                newtitle = "";
                Check_MAT();
                Exp = $("#dExpA").hasClass("nomat");

                if ((noMat != "") && (Exp == true)) {
                    newtitle = 'Topped Cord A is expired and Topped Cord B don’t match Topped Cord A.';
                    $("#dialog-c").parent().find("span.ui-dialog-title").html(newtitle);
                    open_dialogc();
                } else if (Exp) {
                    newtitle = 'Topped Cord A is expired.';
                    $("#dialog-c").parent().find("span.ui-dialog-title").html(newtitle);
                    open_dialogc();
                } else if (noMat != "") {
                    newtitle = 'Topped Cord B don’t match Topped Cord A.';
                    open_dialogc();
                }
                else Savedata();
            }

            function open_dialogc() {
                $("#dialog-c").delay(500)
                                    .queue(function () {
                                        $("#dialog-c").dialog("open").dequeue();
                                    })
            }

            function Savedata() {

                if ($("#txtSize").val() == "") {
                    return;
                }
                if ($("#txtForming").val() == "") {
                    return;
                }
                if ($("#txtInputA").val() == "") {
                    return;
                }
                if ($("#txtInputB").val() == "") {
                    return;
                }
                var myArrayA = $("#txtInputA").val().split("+");
                var reason = "";

                if ($.trim($("#txtshort").val()) != "") {
                    reason = newtitle;
                }
                var dataForSubmit = new Array();
                dataForSubmit.push({
                    _Topcrdcd: $("#dCodeA").text(),
                    _Size: $("#txtSize").val(),
                    _Forming: $("#txtForming").val(),
                    _Empname: '<%: User.Identity.Name %>',
                    _Tcrdcd: myArrayA[0],
                    _Tcrdlot: myArrayA[1],
                    _topexp: myArrayA[4],
                    _toplen: myArrayA[6],
                    _toproll: myArrayA[2],
                    _topauthemp: $("#txtempnm").val(),
                    _topreatxt: reason,
                    _topshtxt: $("#txtshort").val()
                });
                var getUsersUrl = '<%= Url.Action("Ins_Sample") %>';

                callScriptMethod(getUsersUrl, { theData: dataForSubmit }, 'ADD');
            }

            ///  Dialog  ///////
            $("#dialog-a").dialog({
                width: 600,
                height: 500,
                autoOpen: false,
                show: "blind",
                resizable: false, //สั่งให้ไม่สามารถย่อขยายได้
                modal: true,  //สั่งให้มีฉากเบลอๆด้านหลัง dialog
                open: function (event, ui) {
                    $(this).parent().children(".ui-widget-header").css("background", "#B22222");
                    $(this).parent().children(".ui-dialog-titlebar").css("color", "white");
                    var myArray = $("#txtInputA").val().split('+');
                    $("#dcode").text(myArray[0]);
                    $("#dLot").text(myArray[1]);
                    $("#dexp").text(myArray[4]);
                    $("#rollno").text(myArray[2]);
                    $("#dLen").text(myArray[6]);
                    $("#txtempnm").val("");
                    $("#txtpass").val("");
                    $("#txtshort").val("");
                },
                buttons: {
                    "Back": function () {
                        $("#txtInputA").val("");
                        $("#txtInputB").val("");
                        $("#txtInputA").focus();
                        $(this).dialog("close");
                        dialog = "";
                    },
                    "Use Tire Cord": function () {
                        $(this).dialog("close");
                        dialog = "A"
                        $("#dialog-b").dialog("open");
                    }
                }
            });

            $("#dialog-b").dialog({
                width: 650,
                height: 400,
                autoOpen: false,
                show: "blind",
                buttons: {
                    "Back": function () {
                        $("#txtInputA").val("");
                        $("#txtInputB").val("");
                        $("#txtInputA").focus();
                        $(this).dialog("close");
                    },
                    "OK": function () {

                        if ($("#txtempnm").val() == "") {
                            alert("ระบุข้อมูลไม่ครบ");
                            $("#TCLen").focus();
                            return;
                        }
                        if ($("#txtpass").val() == "") {
                            alert("ระบุข้อมูลไม่ครบ");
                            $("#TCLen").focus();
                            return;
                        }
                        if ($("#txtshort").val() == "") {
                            alert("ระบุข้อมูลไม่ครบ");
                            $("#TCLen").focus();
                            return;
                        }

                        $.post('<%=Url.Action("Get_Author","BiasHead") %>' + '?users=' + $("#txtempnm").val() + '&pass=' + $("#txtpass").val()
               , function (data) {
                   if (data.success == 0) {
                       alert(data.message);
                   }
                   else {
                       if (dialog == "A") {
                           $("#reaExp").val($("#txtshort").val());
                           Check_MAT();
                       }
                       if (dialog == "B") {
                           $("#txtreaMat").val($("#txtshort").val());
                       }

                       Savedata();
                       $("#dialog-b").dialog("close");
                   }
               });
                    }
                }
            });


            $("#dialog-c").dialog({
                width: 800,
                height: 600,
                autoOpen: false,
                show: "blind",
                resizable: false, //สั่งให้ไม่สามารถย่อขยายได้
                modal: true,  //สั่งให้มีฉากเบลอๆด้านหลัง dialog
                open: function (event, ui) {
                    $(this).parent().children(".ui-widget-header").css("background", "#B22222");
                    $(this).parent().children(".ui-dialog-titlebar").css("color", "white");
                    var myArray = $("#txtInputA").val().split('+');
                    var myArrayB = $("#txtInputB").val().split('+');

                    $("#dCodeA").text(myArray[0]);
                    $("#dLotA").text(myArray[1]);
                    $("#dExpA").text(myArray[4]);
                    $("#dRollA").text(myArray[2]);


                    $("#dCodeB").text(myArrayB[0]);
                    $("#dLotB").text(myArrayB[1]);
                    $("#dExpB").text(myArrayB[4]);
                    $("#dRollB").text(myArrayB[2]);

                    $("#txtempnm").val("");
                    $("#txtpass").val("");
                    $("#txtshort").val("");
                },
                buttons: {
                    "Back": function () {
                        $("#txtInputA").val("");
                        $("#txtInputB").val("");
                        $("#txtInputA").focus();
                        $(this).dialog("close");

                    },
                    "Use Tire Cord": function () {
                        $(this).dialog("close");
                        dialog = "C"
                        $("#dialog-b").dialog("open");
                    }
                }
            });

            $("#back").click(function () {

                var url = '<%: Url.Action("Header_line","BiasHead")%>';
                window.location = url;
            });

            function callScriptMethod(url, jsonObject, action, callback, async) {

                callback = callback || function () { };
                async = (async == null || async);

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: url,
                    data: JSON.stringify(jsonObject),
                    dataType: "json",
                    async: async,
                    success: function (data) {

                        if (data.success != "0") {
                            var url = '<%: Url.Action("New_Sample")%>';
                            window.location = url + '?seq=' + data.subitem;

                        } else alert(data.message);
                    },
                    error: function (xhr, status, error) {
                        var err = xhr.responseText;
                        alert(err.Message);
                    }
                });
            }


        });
    </script>
</asp:Content>
